// var ejs = require('ejs');
(function(window) {

    //自定義擴展template方法
    String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); }

    var SINGER_CONFIG = {
        GET_SINGER: './data/22.json'
    };

    var apiCallback = function(url) {
        url += '';
        return url + (/\?/.test(url) ? '&v=' : '?v=') + Date.now()
    }

    //使用简单的js引擎模板
    //ejs.render
    //请求ajax参数数据
    var url = apiCallback(SINGER_CONFIG.GET_SINGER);
    $.getJSON(url, function(res) {
        if (res) {
            var mainTpl = ejs.render($('#mainTpl').html(), res);
            var bannerTpl = ejs.render($('#bannerTpl').html(), res);
            $('#mainWrap').html(mainTpl);
            $('#bannerWarp').html(bannerTpl);
            $('body').add('.mainWrap,.ft-bd').css('background', res.profile.bgColor);
            singerInit(res.fb)
        }
    })

    //初始化藝人的信息
    var singerInit = function(fb) {
        //fb登陆方法
        window.fbAsyncInit = function() {
            FB.init({
                appId: fb ? fb.appId : '1382678158693829',
                xfbml: fb ? fb.xfbml : true,
                version: fb ? fb.version : 'v2.4'
            });
            $('.btn-share').click(function(e) {
                e.preventDefault();
                FB.ui({
                    method: 'share',
                    href: fb ? fb.fbShareUrl : 'http://rcshow.tv/activity/cover_singer/22/'
                }, function(res) {
                    console.log(res)
                });
            });
        };

        (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s);
            js.id = id;
            js.src = "//connect.facebook.net/zh_CN/sdk.js#xfbml=1&version=v2.4";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));

        $(function() {
            if ($(window).width() > 465) {
                $("#scrollable1").scrollable();
            } else {
                $("#scrollable2").scrollable();
            }
        });
    }

})(window)